package com.mifengkong.frwidget.recyclerview;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.mifengkong.frwidget.R;
import com.mifengkong.frwidget.loadmore.OnScrollToBottomLoadMoreListener;
import com.mifengkong.frwidget.recyclerview.divider.RecyclerViewDivider;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;


/**
 *
 */

public class PtrRecyclerViewUIComponent extends PtrClassicFrameLayout {

    private boolean canRefresh = true;

    public PtrRecyclerViewUIComponent(Context context) {
        super(context);
    }

    public PtrRecyclerViewUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PtrRecyclerViewUIComponent(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private RecyclerView mRecyclerView;

    private OnScrollToBottomLoadMoreListener mOnScrollToBottomLoadMoreListener;

    private OnPullToRefreshListener mOnPullToRefreshListener;

    public void setOnScrollToBottomLoadMoreListener(OnScrollToBottomLoadMoreListener mOnScrollToBottomLoadMoreListener) {
        this.mOnScrollToBottomLoadMoreListener = mOnScrollToBottomLoadMoreListener;
    }

    public void setOnPullToRefreshListener(OnPullToRefreshListener mOnPullToRefreshListener) {
        this.mOnPullToRefreshListener = mOnPullToRefreshListener;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    @Override
    public void setSmlieViewColor(int colorInt){
        super.setSmlieViewColor(colorInt);
    }

    private void init() {
        disableWhenHorizontalMove(true);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_uicomponent);
        setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (null != mOnPullToRefreshListener) {
                    mOnPullToRefreshListener.onPullToRefresh();
                }
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                if (!canRefresh) {
                    return false;
                }
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }
        });

        setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                if (null != mOnScrollToBottomLoadMoreListener) {
                    mOnScrollToBottomLoadMoreListener.onScrollToBottomLoadMore();
                }
            }
        });
    }

    public void setBackground(int color) {
        setBackgroundColor(ContextCompat.getColor(getContext(), color));
    }

    public void setRecyclerViewOverScrollModeNever(){
        mRecyclerView.setOverScrollMode(OVER_SCROLL_NEVER);
    }

    /**
     * 重新设置RecyclerView的布局
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param clipChildren
     */
    public void setRecyclerViewLayoutParmas(int left, int top, int right, int bottom, boolean clipChildren) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        params.setMargins(left, top, right, bottom);
        mRecyclerView.setLayoutParams(params);
        setClipChildren(clipChildren);
    }

    public void setRecyclerViewLayoutParams(int left, int top, int right, int bottom) {
        setRecyclerViewLayoutParmas(left, top, right, bottom, true);
    }

    public void loadMoreComplete() {
        loadMoreComplete(true);
    }

    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    public <T extends RecyclerView.LayoutManager> void setLayoutManager(T layoutManager) {
        mRecyclerView.setLayoutManager(layoutManager);
    }

    public void setItemAnimator() {
        mRecyclerView.getItemAnimator().setChangeDuration(0);
    }

    public <T extends RecyclerView.Adapter> void setAdapter(T adapter) {
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 添加分割线
     *
     * @param divider 自定义的分割线
     */
    public void setRecyclerViewDivider(RecyclerViewDivider divider) {
        mRecyclerView.addItemDecoration(divider);
    }
    /*
    * 添加分割线
    * */
    public void addItemDivider(RecyclerView.ItemDecoration itemDecoration){
        mRecyclerView.addItemDecoration(itemDecoration);
    }

    public void delayRefresh() {
        delayRefresh(100);
    }

    public void delayRefresh(int delayTime) {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                autoRefresh();
            }
        }, delayTime);
    }


    public void setCanRefresh(boolean canRefresh) {
        this.canRefresh = canRefresh;
    }

}